from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_sale.service.packaging_service import PackagingService
from module_sale.entity.vo.packaging_vo import DeletePackagingModel, PackagingModel, PackagingPageQueryModel, \
    PackagingAddModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


packagingController = APIRouter(prefix='/sale/packaging', dependencies=[Depends(LoginService.get_current_user)])


@packagingController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:list'))]
)
async def get_price_packaging_list(
    request: Request,
    packaging_page_query: PackagingPageQueryModel = Depends(PackagingPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    packaging_page_query_result = await PackagingService.get_packaging_list_services(query_db, packaging_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=packaging_page_query_result)


@packagingController.post('', dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:add'))])
@ValidateFields(validate_model='add_packaging')
@Log(title='包装费用管理', business_type=BusinessType.INSERT)
async def add_price_packaging(
    request: Request,
    add_packaging: PackagingAddModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_packaging.create_by = current_user.user.user_name
    add_packaging.create_time = datetime.now()
    add_packaging.update_by = current_user.user.user_name
    add_packaging.update_time = datetime.now()
    add_packaging_result = await PackagingService.add_packaging_services(query_db, add_packaging, request)
    logger.info(add_packaging_result.message)

    return ResponseUtil.success(msg=add_packaging_result.message)


@packagingController.put('', dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:edit'))])
@ValidateFields(validate_model='edit_packaging')
@Log(title='包装费用管理', business_type=BusinessType.UPDATE)
async def edit_price_packaging(
    request: Request,
    edit_packaging: PackagingModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_packaging.update_by = current_user.user.user_name
    edit_packaging.update_time = datetime.now()
    edit_packaging_result = await PackagingService.edit_packaging_services(query_db, edit_packaging)
    logger.info(edit_packaging_result.message)

    return ResponseUtil.success(msg=edit_packaging_result.message)


@packagingController.delete('/{packaging_ids}', dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:remove'))])
@Log(title='包装费用管理', business_type=BusinessType.DELETE)
async def delete_price_packaging(request: Request, packaging_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_packaging = DeletePackagingModel(packagingIds=packaging_ids)
    delete_packaging_result = await PackagingService.delete_packaging_services(query_db, delete_packaging)
    logger.info(delete_packaging_result.message)

    return ResponseUtil.success(msg=delete_packaging_result.message)


@packagingController.get(
    '/{packaging_id}', response_model=PackagingModel, dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:query'))]
)
async def query_detail_price_packaging(request: Request, packaging_id: int, query_db: AsyncSession = Depends(get_db)):
    packaging_detail_result = await PackagingService.packaging_detail_services(query_db, packaging_id)
    logger.info(f'获取packaging_id为{packaging_id}的信息成功')

    return ResponseUtil.success(data=packaging_detail_result)


@packagingController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('sale:packaging:export'))])
@Log(title='包装费用管理', business_type=BusinessType.EXPORT)
async def export_price_packaging_list(
    request: Request,
    packaging_page_query: PackagingPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    packaging_query_result = await PackagingService.get_packaging_list_services(query_db, packaging_page_query, is_page=False)
    packaging_export_result = await PackagingService.export_packaging_list_services(packaging_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(packaging_export_result))
